/**
 * ngTable: Table + Angular JS
 *
 * @author Vitalii Savchuk <esvit666@gmail.com>
 * @url https://github.com/esvit/ng-table/
 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
 */

(function(){
    'use strict';

    angular.module('ngTable')
        .directive('ngTableColumnsBinding', ngTableColumnsBinding);

    ngTableColumnsBinding.$inject = ["$parse"];

    /**
     * @ngdoc directive
     * @name ngTableColumnsBinding
     * @description One-way data binds the $columns array generated by ngTable/ngTableDynamic to the specified
     * expression.
     *
     * This allows the $columns array to be accessed outside of the html table markup
     */
    function ngTableColumnsBinding($parse){
        var directive = {
            restrict: 'A',
            require: 'ngTable',
            link: linkFn
        };
        return directive;

        function linkFn($scope, $element, $attrs){
            var setter = $parse($attrs.ngTableColumnsBinding).assign;
            if (setter){
                $scope.$watch('$columns', function(newColumns){
                    var shallowClone = (newColumns || []).slice(0);
                    setter($scope, shallowClone);
                });
            }
        }
    }
})();
